package com.yxw.live_vod_backend_boot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxw.live_vod_backend_boot.dto.ProductCategoryDTO;
import com.yxw.live_vod_backend_boot.dto.ProductCategoryQueryDTO;
import com.yxw.live_vod_backend_boot.dto.ProductCategoryUpdateDTO;
import com.yxw.live_vod_backend_boot.entity.YxProductCategoryEntity;
import com.yxw.live_vod_backend_boot.mapper.ProductCategoryMapper;
import com.yxw.live_vod_backend_boot.service.ProductCategoryService;
import com.yxw.live_vod_backend_boot.util.PageUtils;
import com.yxw.live_vod_backend_boot.vo.ProductCategoryVO;
import com.yxw.yxnet_cd_center.common.dto.PageVO;
import com.yxw.yxnet_cd_center.common.exception.BaseException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 商品类目管理
 * @author xiexuxin
 * @date 2023-12-06
 */
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, YxProductCategoryEntity> implements ProductCategoryService {

    @Autowired
    private ProductCategoryMapper categoryMapper;

    /**
     * 添加商品类目
     * @param categoryDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addCategory(ProductCategoryDTO categoryDTO) {
        // 判断名称是否重复
        YxProductCategoryEntity existCategory = baseMapper.selectOne(new QueryWrapper<YxProductCategoryEntity>().eq("category_name", categoryDTO.getCategoryName()));
        if (existCategory != null) {
            throw new BaseException("【"+categoryDTO.getCategoryName()+"】类目名称已重复，请重新输入");
        }
        YxProductCategoryEntity category = new YxProductCategoryEntity();
        category.setCategoryName(categoryDTO.getCategoryName());
        category.setIsEnable(categoryDTO.getIsEnable());
        category.setCreateTime(new Date());
        baseMapper.insert(category);
    }

    /**
     * 修改商品类目
     * @param categoryDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCategory(ProductCategoryUpdateDTO categoryDTO) {
        YxProductCategoryEntity category = categoryMapper.selectById(categoryDTO.getCategoryId());
        if (category == null){
            throw new BaseException("类目不存在,服务器忙,请稍微再试!");
        }
        // 判断名称是否重复
        YxProductCategoryEntity existCategory = baseMapper.selectOne(new QueryWrapper<YxProductCategoryEntity>().eq("category_name", categoryDTO.getCategoryName()));
        if (existCategory != null) {
            throw new BaseException("【"+categoryDTO.getCategoryName()+"】类目名称已重复，请重新输入");
        }
        category.setCategoryName(categoryDTO.getCategoryName());
        category.setIsEnable(categoryDTO.getIsEnable());
        category.setUpdateTime(new Date());
        baseMapper.updateById(category);
    }

    /**
     * 获取商品类目列表
     * @param queryDTO
     * @return
     */
    @Override
    public PageVO<ProductCategoryVO> getCategoryList(ProductCategoryQueryDTO queryDTO) {
        Page<YxProductCategoryEntity> page = new Page<>(queryDTO.getCurrPage(), queryDTO.getPageSize());
        String categoryName = StringUtils.trimToNull(queryDTO.getCategoryName());
        LambdaQueryWrapper<YxProductCategoryEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(categoryName), YxProductCategoryEntity::getCategoryName, categoryName)
                    .eq(queryDTO.getIsEnable() != null, YxProductCategoryEntity::getIsEnable, queryDTO.getIsEnable());
        page = baseMapper.selectPage(page, queryWrapper);
        List<YxProductCategoryEntity> entityList = page.getRecords();
        List<ProductCategoryVO> listVoList = new ArrayList<>();
        if (null != entityList && !entityList.isEmpty()) {
            listVoList = BeanUtil.copyToList(entityList, ProductCategoryVO.class);
        }
        return PageUtils.get(page, listVoList);
    }

    /**
     * 获取商品类目详情
     * @param categoryId
     * @return
     */
    @Override
    public ProductCategoryVO getCategoryById(String categoryId) {
        YxProductCategoryEntity category = categoryMapper.selectById(categoryId);
        ProductCategoryVO vo = new ProductCategoryVO();
        BeanUtil.copyProperties(category, vo);
        return vo;
    }

    /**
     * 商品类目启用/禁用
     * @param categoryId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void isEnableCategoryById(String categoryId) {
        categoryMapper.isEnableCategoryById(categoryId);
    }
}
